package com.fast.config;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import com.fast.modules.acd.exception.SkillNotSupportException;
import com.fast.modules.acd.service.AssignService;
import com.fast.modules.acd.service.QueueStorageService;
import com.fast.modules.acd.service.SeatStorageService;
import com.fast.modules.acd.service.impl.AssignServiceImpl;
import com.fast.modules.acd.service.impl.jvm.QueueStorageServiceJVMImpl;
import com.fast.modules.acd.service.impl.jvm.SeatStorageServiceJVMImpl;

import lombok.extern.slf4j.Slf4j;

/**
 * ACD相关的spring 配置类
 * @author yaoyuquan
 *
 */
@Configuration
@EnableScheduling
@Slf4j
public class ACDConfig {
	
	
	@Bean("sessionMap")
	public Map<String, String> sessionMap() {
		return new HashMap<>();
	}
	
	/**
	 * 默认是最长空闲时间分配
	 */
	@Value("${acd.assign.strategy}")
	String assignStratrgy;
	
	/**
	 * 
	 * @return
	 */
	@Bean
	AssignService assignService() {
		
		log.debug("当前的分配规则是：" + assignStratrgy);
		
		return new AssignServiceImpl();
	}
	
	
	@Bean
	QueueStorageService queueStorageService() {
		return new QueueStorageServiceJVMImpl();
	}
	
	
	@Bean
	SeatStorageService seatStorageService() {
		return new SeatStorageServiceJVMImpl();
	}
	
	@Scheduled(fixedDelay=5000)
	public void runAssign() {
		try {
			assignService().assign();
			
		} catch (SkillNotSupportException e) {
			log.error(e.getMessage());
		} catch (IOException e) {
			log.error(e.getMessage());
		}
	}
	
}
